import React from 'react'
//需要使用store中的值
import store from "../../store/store"


export default function Count() {
    //getState取store中保存的值
    const {count} = store.getState()
    const incrementHandle = ()=>{
        //有需求.dispatch方法可以将需求传递给仓库
        store.dispatch({
            type: "increamentCount",
            data: 1
        })

    }

    const decreamentHandle = ()=>{
        store.dispatch({
            type: "decreamentCount",
            data: 1
        })
    }

    const ifOddDecrementHandle = ()=>{
        if(count % 2 !==0) {
            store.dispatch({
                type: "increamentCount",
                data: 1
            })
        }
    }

    const waitDecrementHandle = ()=>{
        setTimeout(()=>{
            store.dispatch({
                type: "increamentCount",
                data: 1
            })
        },2000)
    }
    return (
        <div>
            <h1>我是Count组件 我的值是{count}</h1>
            <div>
                <button onClick={incrementHandle}>累加</button>
                <button onClick={decreamentHandle}>累减</button>
                <button onClick={ifOddDecrementHandle}>如果是奇数再加1</button>
                <button onClick={waitDecrementHandle}>等2s再加1</button>
            </div>
        </div>
    )
}
